<?php
	class AdminDthink_CategoryController extends Main_Controller_Action
	{
		public function init()
		{
			$this->loadTemplates('admin');
			$db = Zend_Registry::get('db');
		}
		
		public function preDispatch()
		{
			$auth = Zend_Auth::getInstance();
			if((!$auth->hasIdentity()) || ($auth->getIdentity()->level != 0))
			{
				if($this->_request->getActionName() != 'login')
				{
					$this->_redirect('/admin-dthink/index/login');
				}
			}
		}
		
		public function indexAction()
		{
			$this->view->headTitle('Quản lý danh mục');
			$category = new AdminDthink_Model_Category();
			
			if($_POST['add'])
			{
				$this->_redirect('admin-dthink/category/create');
			}			
			if($_POST['orders'])
			{
				$result = $category->listCategory();
				$data = array();
				foreach ($result as $row)
				{
					$data[] = $row;
				}
				$orderArr = $this->_request->getParam('ordering');
				$category->orderTree($data, $orderArr);
			}
			if($_POST['refresh'])
			{
				$this->_redirect('admin-dthink/category');
			}
			
			$result = $category->listItem(0, 'all', 0);			
			$orderArr = $category->orderGroup($result);
			
			$this->view->total = count($result);
			$this->view->data = $result;
		}
		
		public function createAction()
		{
			$this->view->headTitle('Tạo danh mục mới');
			$category = new AdminDthink_Model_Category();
			$result = $category->listCategory();
			$this->view->data = $result;
			
			if($this->_request->isPost('add'))
			{
			    $filter = new Zend_Filter_StringTrim();
				$cat_name = $filter->filter($this->_request->getParam('cat_name'));
				$parents  = $this->_request->getParam('parents');
                $link     = $filter->filter($this->_request->getParam('link'));
				$flag = true;
				
				$validation = new AdminDthink_Form_ValidateCategory();
				if(!$validation->validCategoryName($cat_name))
				{
					$this->view->errorCatName = 'Tên danh mục không được để trống và tối đa 100 kí tự';
					$flag = false;
				}
				
				if($flag == true)
				{
				    if($link != '')
                    {
                        $data = array(
    						'cat_name' => $cat_name,
                            'link' => $link,
    						'status' => '1',
                        );
                    }
					else
                    {
                        $data = array(
    						'cat_name' => $cat_name,
    						'status' => '1',
                        );
                    }
					$category->insertNode($data, $parents, array('position' => 'right'));
					$this->_redirect('admin-dthink/category');
				}
			}
		}
		
		public function editAction()
		{
			$this->view->headTitle('Sửa thông tin danh mục');
			$category = new AdminDthink_Model_Category();
			$cat_id = $this->_request->getParam('id');
			$getCat = $category->getNodeInfo($cat_id);
			$this->view->category = $getCat;
			
			$listCat = $category->listCategory();
			$this->view->data = $listCat;
			
			if($this->_request->isPost())
			{
                $filter = new Zend_Filter_StringTrim();
				$cat_name	= $filter->filter($this->_request->getParam('cat_name'));
				$parents	= $this->_request->getParam('parents');
				$status		= $this->_request->getParam('status');
                $link       = $filter->filter($this->_request->getParam('link'));
				$flag = true;
				
				$validation = new AdminDthink_Form_ValidateCategory();
				if(!$validation->validCategoryName($cat_name))
				{
					$this->view->errorCatName = 'Tên danh mục không được để trống và tối đa 100 kí tự';
					$flag = false;
				}
				
				if($flag == true)
				{
                    $data = array(
        				'cat_name' => $cat_name,
        				'status'   => $status,
                        'link'     => $link,
    				);
					
					$category->updateNode($data, $cat_id, $parents);
					$this->_redirect('admin-dthink/category');
				}
			}
		}
		
		public function deleteAction()
		{
			$delete = new AdminDthink_Model_Category();
			$cat_id = $this->_request->getParam('id');
			$result = $delete->removeNode($cat_id);
			$this->_redirect('admin-dthink/category');
		}
		
		public function searchAction()
		{
			$this->view->headTitle('Tìm kiếm danh mục');
			$filter = new Zend_Filter_StringTrim();
			$keyword = $filter->filter($this->_request->getParam('keyword'));
			$category = new AdminDthink_Model_Category();
			$flag = true;
			
			if($this->_request->isPost())
			{
				$validation = new AdminDthink_Form_ValidateCategory();
				if($validation->validCategorySearch($keyword))
				{
					$this->view->searchValue = $keyword;
				}
				else
				{
					$this->view->errorSearch = 'Bạn chưa nhập từ khóa';
					$result = $category->searchCategory($keyword);
					$orderArr = $category->orderGroup($result);
						
					$this->view->total = count($result);
					$this->view->data = $result;
					$flag = false;
				}
			}
				
			if($_POST['add'])
			{
				$this->_redirect('admin-dthink/category/create');
			}
			if($_POST['orders'])
			{
				$result = $category->listCategory();
				$data = array();
				foreach ($result as $row)
				{
					$data[] = $row;
				}
				$orderArr = $this->_request->getParam('ordering');
				$category->orderTree($data, $orderArr);
			}
			if($_POST['refresh'])
			{
				$this->_redirect('admin-dthink/category');
			}
			
			$validation = new AdminDthink_Form_ValidateCategory();
			if($validation->validCategorySearch($keyword))
			{
				$this->view->searchValue = $keyword;
			}
			else
			{
				$this->view->errorSearch = 'Bạn chưa nhập từ khóa';
				$result = $category->searchCategory($keyword);
				$orderArr = $category->orderGroup($result);
						
				$this->view->total = count($result);
				$this->view->data = $result;
				$flag = false;
			}
			
			if($flag == true)
			{
				$result = $category->searchCategory($keyword);
				$orderArr = $category->orderGroup($result);
				
				$this->view->total = count($result);
				$this->view->data = $result;
			}
		}
	}
